<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>-->

      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" size="mini" v-if="scope.row.pay_status == 0">未付款</el-tag>
            <el-tag type="success" size="mini" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | ellipsis }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220px">
          <template slot-scope="scope">
            <el-row>
              <el-button
                size="mini"
                icon="el-icon-s-data"
                @click="showOrderDetailDialog(scope.row.order_id)"
              >查看订单</el-button>
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-sell"
                @click="showSendOrderDialog(scope.row)"
                v-show="scope.row.is_send === '否' && scope.row.pay_status === '1'"
              >发货</el-button>
              <el-button
                type="success"
                size="mini"
                icon="el-icon-location"
                v-show="scope.row.is_send === '是' && scope.row.pay_status === '1'"
                @click="showProgressDialog"
              >物流</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流进度对话框 -->
    <el-dialog title="查看物流进度" :visible.sync="progressDialogVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
    <!-- 确认发货对话框 -->
    <el-dialog title="发货" :visible.sync="dialogVisible" width="30%">
      <span>确认发货？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendOrder">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 订单详情对话框 -->
    <el-dialog title="订单详情" :visible.sync="showOrderDetail" width="50%">
      <div class="form">
        <div class="title">
          订单编号：
          <span class="label">{{orderDetail.order_number}}</span>
        </div>
        <div class="title">
          收件人:
          <span class="label" style="margin-left: 30px">{{orderDetail.order_fapiao_company}}</span>
        </div>
        <div class="title">
          电话号码：
          <span class="label">{{orderDetail.order_fapiao_content}}</span>
        </div>
        <div class="title">
          收件地址：
          <span class="label">{{orderDetail.consignee_addr}}</span>
        </div>
        <div class="title">
          订单金额：
          <span class="label">{{orderDetail.order_price}}</span>
        </div>
        <div class="title">
          付款状态：
          <el-tag type="danger" size="mini" v-if="orderDetail.pay_status == 0">未付款</el-tag>
          <el-tag type="success" size="mini" v-else>已付款</el-tag>
        </div>
        <div class="title" v-show="orderDetail.pay_status == '1'">
          付款方式：
          <span
            class="label"
          >{{orderDetail.order_pay == '1' ? '支付宝' : orderDetail.order_pay == '2' ? '微信' : orderDetail.order_pay == '3' ? '银行卡' : ''}}</span>
        </div>
        <div class="title" v-show="orderDetail.pay_status == '1'">
          是否发货：
          <span class="label">{{orderDetail.is_send}}</span>
        </div>
        <div class="title" style="display:flex; flex-wrap:wrap">订单商品：</div>
        <div class="title">
          <div class="goods" v-for="(good,index) in orderDetail.goods" :key="index">
            <div class="left">
              <img :src="good.goods_pics" />
            </div>
            <div class="right">
              <div class="right_top">{{good.goods_name}}</div>
              <div class="right_bottom">x{{good.goods_number}}</div>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-sell"
          @click="showSendOrderDialog(orderDetail)"
          v-show="orderDetail.is_send === '否' && orderDetail.pay_status === '1'"
        >发货</el-button>
        <el-button
          type="success"
          size="mini"
          icon="el-icon-location"
          v-show="orderDetail.is_send === '是' && orderDetail.pay_status === '1'"
          @click="showProgressDialog"
        >物流</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      // 订单列表查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 订单列表
      orderList: [],
      // 修改地址对话框
      addressDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityData,
      // 物流进度对话框
      progressDialogVisible: false,
      // 物流进度
      progressInfo: [],
      // 发货对话框
      dialogVisible: false,
      // 订单详情对话框
      showOrderDetail: false,
      order: {},
      orderDetail: {},
      orderId: 0,
    }
  },
  filters: {
    ellipsis(value) {
      let time = new Date(value * 1000);
      let y = time.getFullYear();
      let m = time.getMonth() + 1;
      let d = time.getDate();
      let h = time.getHours();
      let mm = time.getMinutes();
      let s = time.getSeconds();
      function add0(m) {
        return m < 10 ? '0' + m : m
      }
      return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    // 分页
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange(newSize) {
      this.queryInfo.pagenum = newSize
      this.getOrderList()
    },
    showSendOrderDialog(order) {
      this.dialogVisible = true
      this.orderId = order.order_id
      this.order = order
    },
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    async showProgressDialog() {
      // 供测试的物流单号：1106975712662
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败!')
      }
      this.progressInfo = res.data
      this.progressDialogVisible = true
    },
    sendOrder() {
      let data = JSON.parse(JSON.stringify(this.order))
      data.update_time = Date.parse(new Date()) / 1000,
        data.order_number = 1
      data.is_send = '是'
      console.log(data);
      this.$http.put(`orders/${this.orderId}`, data).then(res => {
        this.$message({
          message: '发货成功',
          type: 'success'
        });
        this.dialogVisible = false
        this.showOrderDetail = false
        this.getOrderList()
      })
        .catch(rej => {
          this.$message.error('发货失败');
        })
    },
    showOrderDetailDialog(id) {
      console.log(id);
      this.$http.get(`orders/${id}`).then(res => {
        console.log(res.data.data);
        this.orderDetail = res.data.data
      })
        .catch(rej => {
        })
      this.showOrderDetail = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
.form {
  .title {
    margin: 10px 0;
    font-size: 18px;
    .label {
      font-size: 15px;
    }
  }
  .goods {
    width: 400px;
    height: 100px;
    margin: 5px 0;
    border: 1px solid #ccc;
    border-radius: 5px;
    display: flex;
    justify-content: space-between;
    .left {
      img {
        width: 100px;
        height: 100px;
      }
    }
    .right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .right_top {
        font-size: 15px;
        margin: 15px 5px 0 0;
        // overflow: hidden;
        // text-overflow: ellipsis;
        // display: -webkit-box;
        width: 200px;
        height: 30px;
        text-align: right;
        margin-right: 5px;
      }
      .right_bottom {
        font-size: 15px;
        margin: 0 5px 15px 0;
        text-align: right;
        margin-right: 15px;
      }
    }
  }
  // .goods:nth-child(n + 2) {
  //   margin-left: 15px;
  // }
}
</style>
